<?
if(isset($_POST['type'])){
    if($_POST['type']=="web"){
        include('../libraries/data.lib.php');
        $filename='http://download.symbiosis.webex.am/index.php';
        $f=@fopen($filename, 'r');
        if($f){
            $contents = '';
            while (!feof($f)){
              $contents .= fread($f, 8192);
            }
            fclose($f);
            print($contents);
        }
    }
    elseif($_POST['type']=="archive"){
        if($dir=opendir('../../')){
            while($file=readdir($dir)){
                if(substr($file, 0, 9)==="symbiosis"){
                    $len=strlen($file);
                    $version=substr($file, 10, $len-14);
                }
            }
            closedir($dir);
        }
        if(isset($version)) print('{"version": "'.$version.'"}');
        else print('{"version": ""}');
    }
    elseif($_POST['type']=="config"){
        if(file_exists('../../index.php')){
            $f=@fopen('../../index.php', 'r');
            if(!$f) exit();
            $contents = '';
            while (!feof($f)){
              $contents .= fread($f, 8192);
            }
            fclose($f);
            preg_match("/Symbiosis ([0-9.]*)/", $contents, $path);
            $version=$path[1];
        }
        if(isset($version)) print('{"version": "'.$version.'"}');
        else print('{"version": ""}');
    }
    elseif($_POST['type']=="install_archive"&&isset($_POST['version'])){
        include('../libraries/zip.lib.php');
        include('../libraries/data.lib.php');
        $version=data::word($_POST['version'], 'en', 0, '.');
        $zip=new zip('../../symbiosis-'.$version.'.zip');
        $zip->unzip('../../');
        chmod('../../uploads/', 0777);
        print('{"success":true}');
    }
    elseif($_POST['type']=="install_web"&&isset($_POST['version'])){
        include('../libraries/zip.lib.php');
        include('../libraries/data.lib.php');
        $version=data::word($_POST['version'], 'en', 0, '.');
        set_time_limit(120);
        $server='http://download.symbiosis.webex.am/';
        $file='symbiosis-'.$version.'.zip';
        $f=@fopen($server.$file, "r");
        if($f){
            $contents = '';
            while (!feof($f)){
              $contents .= fread($f, 8192);
            }
            fclose($f);
            $f=fopen('../../'.$file, 'w');
            fwrite($f, $contents);
            fclose($f);
            
            $zip=new zip('../../symbiosis-'.$version.'.zip');
            $zip->unzip('../../');
            print('{"success":true}');
        }
        else{
            print('{"success":false}');
        }
    }
}
?>